package com.kehutong.charge.service.impl;

import com.kehutong.charge.entity.ChargeRefundConfig;
import com.kehutong.charge.service.ChargeRefundConfigService;
import com.kehutong.charge.util.enums.EnableStateEnum;
import com.kehutong.common.constant.CommonConstant;
import com.kehutong.common.errcode.Result;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.Page;
import org.coraframework.orm.jdbc.execute.entity.FindPage;

import java.util.List;
import java.util.Objects;

/**
 * @program: kht
 * @description: ChargeRefundConfigServiceImpl
 * @author: joneron
 * @create: 2021/6/29
 **/
public class ChargeRefundConfigServiceImpl implements ChargeRefundConfigService {

    @Inject
    private JdbcSession jdbcSession;




    @Override
    public Page<ChargeRefundConfig> findPage(JSONObject jsonObject) {
        this.initDefaultConfig();
        FindPage<ChargeRefundConfig> find = jdbcSession.findPage(ChargeRefundConfig.class)
                .eq("deleted", false)
                .eq(jsonObject,"communityNo")
                .like(jsonObject,"refundName")
                .eq(jsonObject,"status")
                .order("sort", "desc")
                .order("createTime", "desc");

        find.page(jsonObject);

        return find.exe();
    }


    private void initDefaultConfig(){
        ChargeRefundConfig refundConfig = jdbcSession.findOne(ChargeRefundConfig.class)
                //.eq("deleted", false) 需求变更，可删除
                .eq("communityNo", CommonConstant.DEFAULT_COMMUNITY_NO)
                .eq("initData", true)
                .exe();
        if(null != refundConfig){
            return;
        }
        ChargeRefundConfig config = Pool.newInstance(ChargeRefundConfig.class);
        config.setCommunityNo(CommonConstant.DEFAULT_COMMUNITY_NO);
        config.setRefundName(CommonConstant.DEFAULT_COMMUNITY_NAME);
        config.setCommunityName(CommonConstant.DEFAULT_COMMUNITY_NAME);
        config.setRefundMethod("退回到原支付");
        config.setApproveMethod(EnableStateEnum.ENABLE);
        config.setApplyName("--");
        config.setSort(999);
        config.setInitData(true);
        config.setStatus(true);
        config.setCreateName("System");
        jdbcSession.insert(config);
    }


    @Override
    public List<ChargeRefundConfig> findList(JSONObject jsonObject) {
        return jdbcSession.findArray(ChargeRefundConfig.class)
                .eq("deleted", false)
                .eq("status",true)
                .exe();
    }


    @Override
    public Object save(ChargeRefundConfig chargeRefundConfig) {
        if(CommonConstant.DEFAULT_COMMUNITY_NAME.equals(chargeRefundConfig.getCommunityNo())){
            //如果是添加默认配置
            chargeRefundConfig.setCommunityNo(CommonConstant.DEFAULT_COMMUNITY_NO);
            chargeRefundConfig.setInitData(true);
            chargeRefundConfig.setSort(999);
        }
        //查该小区没有删除的
        ChargeRefundConfig config = jdbcSession.findOne(ChargeRefundConfig.class)
                .eq("communityNo",chargeRefundConfig.getCommunityNo())
                .eq("deleted",false).exe();

        if(null != config){
            return Result.error(1234,"该小区已经配置了退款模板！");
        }
        return jdbcSession.insert(chargeRefundConfig);
    }


    @Override
    public Object update(ChargeRefundConfig chargeRefundConfig) {
        if(chargeRefundConfig.isInitData() && !CommonConstant.DEFAULT_COMMUNITY_NO.equals(chargeRefundConfig.getCommunityNo())){
            throw new RuntimeException("系统初始化数据不能修改ID.");
        }
        return jdbcSession.updateById(chargeRefundConfig);
    }


    @Override
    public Object deleteById(String id) {
        ChargeRefundConfig result = Pool.get(ChargeRefundConfig.class, id);
        if (result != null) {
            result.setDeleted(true);
            jdbcSession.updateById(result);
        }
        return 1;
    }



}
